import axios from "axios";
import Vue from '@/main.js'

function message(res) {
  if(res.data.message == 'needLogin'){
    location.href = '/#/login';
    localStorage.clear();
  }
  Vue.$message({
    showClose: false,
    message: res.data.message,
    type: "error",
    duration: 5000,
    center: true
  });
}

var HttpService = {
  get: function (url, params) {
    return new Promise((resolve, reject) => {
      axios.get(url, params)
        .then(function (response) {
          resolve(response);
        }).catch(function (err) {
          message(err.response);
          reject(err)
        });
    })
  },
  post: function (url, params) {
    return new Promise((resolve, reject) => {
      axios.post(url, params)
        .then(function (response) {
          resolve(response);
        }).catch(function (err) {
          message(err.response);
          reject(err)
        });
    })
  }
}

export default HttpService;